//
//  YODetailScrollView.m
//  YOYOLive
//
//  Created by 王根强 on 2016/10/11.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "YODetailScrollView.h"

@interface YODetailScrollView ()<UIScrollViewDelegate> {
    
    UIButton * selectButton;
    UIView * sliderView;
    NSInteger baseTag;
    UIView * sliderV;
    NSString *sliderViewLocationStr;
    NSString *searchStr;
}
@property (nonatomic, strong) NSMutableArray * buttonArray;


@end

@implementation YODetailScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.buttonArray = [NSMutableArray array];
    }
    return self;
}

- (void)buildWithTitleArray:(NSArray *)titleArray
        withTitleViewHeight:(CGFloat)titleHeight
              withTitleFont:(UIFont *)titleFont
             withTitleColor:(NSArray *)titleColorArray
              withButtonTag:(NSInteger)tags
  withLittleScrollViewColor:(UIColor *)viewColor
         withTitleViewColor:(UIColor *)titleViewColor
     withSliderViewLocation:(NSString *)sliderViewLocation
                 withSearch:(NSString *)search
{
    sliderViewLocationStr = sliderViewLocation;
    searchStr = search;
    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, titleHeight)];
    titleView.backgroundColor = titleViewColor;
    
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, titleView.bottom-1, titleView.width, 1)];
    lineV.backgroundColor = RGB(222, 222, 222);
    [titleView addSubview:lineV];
    
    [self addSubview:titleView];
    baseTag = tags;
    for (int i = 0; i < titleArray.count; i++) {
        UIButton * titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.frame = CGRectMake(titleView.width/titleArray.count*i, 0, titleView.width/titleArray.count, titleHeight);
        [titleButton setTitle:titleArray[i] forState:UIControlStateNormal];
        titleButton.titleLabel.font = titleFont;
        [titleButton setTitleColor:titleColorArray[0] forState:UIControlStateNormal];
        [titleButton setTitleColor:titleColorArray[1] forState:(UIControlStateSelected)];
        titleButton.tag = tags+i;
        [titleButton addTarget:self action:@selector(scrollViewSelectToIndex:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:titleButton];
        if (i == 0) {
            selectButton = titleButton;
            selectButton.selected = YES;
        }
        [self.buttonArray addObject:titleButton];
    }
    //滑块
    if ([sliderViewLocation isEqualToString:@"123"]) {
        sliderV=[[UIView alloc]initWithFrame:CGRectMake(0, titleHeight-5, titleView.width/titleArray.count/3, 1)];
        sliderV.centerX = selectButton.centerX;
        sliderV.backgroundColor = viewColor;
        [titleView addSubview:sliderV];
        sliderView = sliderV;
    }else if([sliderViewLocation isEqualToString:@"321"]){
        sliderV=[[UIView alloc]initWithFrame:CGRectMake(0, titleHeight-1.5*KHEIGHT_6PLUS_SCALE, titleView.width/titleArray.count, 1.5*KHEIGHT_6PLUS_SCALE)];
        sliderV.backgroundColor = viewColor;
        [titleView addSubview:sliderV];
        sliderView = sliderV;
    }
    
    // scrollView
    self.baseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, sliderV.bottom, self.width, self.height-sliderV.bottom)];
    self.baseScrollView.delegate = self;
    self.baseScrollView.pagingEnabled = YES;
    self.baseScrollView.showsHorizontalScrollIndicator = FALSE;
    self.baseScrollView.contentSize = CGSizeMake(self.width*titleArray.count, self.baseScrollView.height);
    [self addSubview:self.baseScrollView];
}

- (void)scrollViewSelectToIndex:(UIButton *)button {
    [self selectButton:button.tag-baseTag];
    [UIView animateWithDuration:0.3 animations:^{
        self.baseScrollView.contentOffset = CGPointMake(self.width * (button.tag-baseTag), 0);
    }];
    
    //发送拖动到哪一个的通知
    if ([searchStr isEqualToString:@"1"]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"DetailScrollView_Notification" object:self userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",button.tag-baseTag],@"index", nil]];
    }
}

//选择某个标题
- (void)selectButton:(NSInteger)index {
    selectButton.selected = NO;
    selectButton = self.buttonArray[index];
    selectButton.selected = YES;
    [UIView animateWithDuration:0.3 animations:^{
        if ([sliderViewLocationStr isEqualToString:@"123"]) {
            sliderView.frame = CGRectMake(selectButton.width*index, selectButton.height-5, selectButton.width/3, 1);
            sliderView.centerX = selectButton.centerX;
        }else if ([sliderViewLocationStr isEqualToString:@"321"]) {
            sliderView.frame = CGRectMake(selectButton.width*index, selectButton.height-1.5*KHEIGHT_6PLUS_SCALE, selectButton.width, 1.5*KHEIGHT_6PLUS_SCALE);
        }
    }];
}

//监听滚动事件判断当前拖动到哪一个了
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / self.width;
    [self selectButton:index];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / self.width;
    //发送拖动到哪一个的通知
    if ([searchStr isEqualToString:@"1"]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"DetailScrollView_Notification" object:self userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",index],@"index", nil]];
    }
}


@end
